package view
{
	import flash.events.Event;
	
	import model.DataProxy;
	import model.ViewProxy;
	
	import org.puremvc.as3.interfaces.IMediator;
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;

	public class MenuViewMediator extends Mediator implements IMediator
	{
		public static const NAME:String 				= "MenuViewMediator";
		
		private var menuView:MenuView;
		
		public function MenuViewMediator( viewComponent:Object = null )
		{
			super( NAME, viewComponent );
		}
		
		override public function onRegister():void
		{
			menuView = new MenuView();
			menuView.addEventListener( MenuView.CLICKED, menuViewClickedHandler );
			menuView.init( dataProxy.vo.menuViewData,
						   viewProxy.vo );
			
			viewProxy.vo.menuView = menuView;
			
			viewComponent.addChild( menuView );
			
			dataProxy.currentViewDataSet( menuView.index );
		}
		
		override public function listNotificationInterests():Array
		{
			return [ MenuView.SHOW ];
		}
		
		override public function handleNotification( notification:INotification ):void
		{
			var name:String = notification.getName();
			var body:Object = notification.getBody();
			
			switch( name )
			{
				case MenuView.SHOW:
					menuView.show();
					break;
			}
		}
		
		private function menuViewClickedHandler( e:Event ):void
		{
			dataProxy.currentViewDataSet( menuView.index );

			sendNotification( MenuView.CHANGED, { type:menuView.data.type } );
		}
		
		private function get dataProxy():DataProxy
		{
			return facade.retrieveProxy( DataProxy.NAME) as DataProxy;
		}
		
		private function get viewProxy():ViewProxy
		{
			return facade.retrieveProxy( ViewProxy.NAME	) as ViewProxy;
		}
	}
}